import os


# 数据集分类后的目录
import cv2
import numpy as np
from keras import models
base_dir = 'G:\\python\\keshe\\train1'

# # 训练、验证、测试数据集的目录
test_dir = os.path.join(base_dir, 'test')



from keras.models import load_model
model = load_model('../data/cats_and_dogs_wei_tiao.h5')


test = os.listdir(test_dir)

images = []

# 获取每张图片的地址，并保存在列表images中
for testpath in test:
    for fn in os.listdir(os.path.join(test_dir, testpath)):
        if fn.endswith('jpg'):
            fd = os.path.join(test_dir, testpath, fn)
            images.append(fd)




def get_input_xy(src=[]):
    pre_x = []
    true_y = []

    class_indices = {'cat': 0, 'dog': 1}

    for s in src:
        input = cv2.imread(s)
        input = cv2.resize(input, (150, 150))
        input = cv2.cvtColor(input, cv2.COLOR_BGR2RGB)
        pre_x.append(input)

        _, fn = os.path.split(s)
        y = class_indices.get(fn[:3])
        true_y.append(y)

    pre_x = np.array(pre_x) / 255.0

    return pre_x, true_y


# 得到规范化图片及true label
pre_x, true_y = get_input_xy(images)
# print(pre_x)



pred_y = model.predict(pre_x)
pred_y = np.int64(pred_y > 0.5)
print(pred_y)

from sklearn.metrics import confusion_matrix
confusion_mat = confusion_matrix(true_y, pred_y)
print(confusion_mat)
TP = confusion_mat[0][0]
FP = confusion_mat[0][1]
FN = confusion_mat[1][0]

print("召回率(Recall):",TP/(TP+FN))
print("精确率(Precision) :",TP/(TP+FP))







